package com.bdsy.system.web.controller;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bdsy.common.annotation.Json;
import com.bdsy.common.utils.Result;
import com.bdsy.common.utils.StringUtil;
import com.bdsy.system.model.User;
import com.bdsy.system.model.UserLog;
import com.bdsy.system.shiro.Encrypt;
import com.bdsy.system.shiro.ShiroUtils;

/**
 * ClassName:UserController
 * Description: TODO 添加描述.
 * @author   Administrator
 * @version  
 * @since    JDK 1.8
 */
@Controller
@RequestMapping(value = {"/sys", "/${web.appPath}"})
public class LoginController {
	
    @Json
	@RequestMapping(value = "/login")
    private Result login(String loginName, String loginPassword) {
    	if(!StringUtil.isEmpty(loginName) && !StringUtil.isEmpty(loginPassword)){
    		try {
	    		loginName=loginName.trim();
	    		loginPassword=loginPassword.trim();
	    		Subject subject = ShiroUtils.getSubject();
	    		//loginPassword = new Sha256Hash(loginPassword).toHex();
	    		loginPassword=Encrypt.md5(loginPassword, loginName);
	        	UsernamePasswordToken token = new UsernamePasswordToken(loginName, loginPassword);
	        	subject.login(token);
			} catch (UnknownAccountException e) { 
		        return Result.error(e.getMessage());
		    } catch (IncorrectCredentialsException e) { 
		        return Result.error(e.getMessage());
		    } catch (LockedAccountException e) {  
		        return Result.error(e.getMessage());
		    } catch (AuthenticationException e) { 
		        return Result.error("账户验证失败");
		    }
    		System.out.println(Result.ok());
    		return Result.ok();
    	}
    	return Result.error("账号或密码不能空");
    }
    
    
    @Json
   	@RequestMapping(value = "/applogin")
       private Object applogin(String loginName, String loginPassword) {
       	if(!StringUtil.isEmpty(loginName) && !StringUtil.isEmpty(loginPassword)){
       		try {
   	    		loginName=loginName.trim();
   	    		loginPassword=loginPassword.trim();
   	    		Subject subject = ShiroUtils.getSubject();
   	    		//loginPassword = new Sha256Hash(loginPassword).toHex();
   	    		loginPassword=Encrypt.md5(loginPassword, loginName);
   	        	UsernamePasswordToken token = new UsernamePasswordToken(loginName, loginPassword);
   	        	subject.login(token);
   			} catch (UnknownAccountException e) { 
   		        return Result.error(e.getMessage());
   		    } catch (IncorrectCredentialsException e) { 
   		        return Result.error(e.getMessage());
   		    } catch (LockedAccountException e) {  
   		        return Result.error(e.getMessage());
   		    } catch (AuthenticationException e) { 
   		        return Result.error("账户验证失败");
   		    }
       		Subject subject = ShiroUtils.getSubject();
    		User user = (User) subject.getPrincipal();
    		Map<String,Object> map = new LinkedHashMap<>();
    		map.put("user", user);
    		map.put("code", 200);
    		map.put("msg", "loginsuccess");
       		return map;
       	}
       	return Result.error("账号或密码不能空");
       }
    
    
	@RequestMapping(value = "/logis")
    private String login() {
		return "redirect:/login.html?code=3";
    }
    
    @RequestMapping(value = "/main")
    private String main(HttpServletRequest request,UserLog userLog) {
		Subject subject = ShiroUtils.getSubject();
		User user = (User) subject.getPrincipal();
		if(user != null){
			/*if ("3".equals(user.getRoleId())) {
				return "redirect:/f/main";
			}else{
				return "/main";
			}*/
			return "/main";
		}else{
			return "redirect:/login.html?code=1";
		}
    }
	
	@RequestMapping(value = "/index")
	private String index(){
		return "/main";
	}
    
    @RequestMapping(value="/lougt")
    public String lougt(HttpServletRequest request){
		ShiroUtils.logout();
    	return "redirect:/login.html";
    }   
}

